//
//  WZResetPasswordController.m
//  ETong
//
//  Created by wanzhao on 15/11/2.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZResetPasswordController.h"
#import "CFolderMgr.h"
#import "UIBarButtonItem+category.h"
#import "MBProgressHUD+WZ.h"
#import "NetWorkShare.h"


@interface WZResetPasswordController ()<UITextFieldDelegate>

@property (nonatomic, weak) UIButton *errorBtn;

@property (nonatomic, weak) UITextField *ewTF;
@property (nonatomic, weak) UITextField *oldTF;

@property (nonatomic, strong) CAccountModel *curAcc;

@end

@implementation WZResetPasswordController

- (void)dealloc
{
    NSLog(@"WZResetPasswordController dealloc");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLORWITH16COLOR(0xeeeeee);
    self.navigationItem.title = @"重置密码";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:nil title:@"修改" target:self sel:@selector(updatePassWord) titleFont:Font15 titleColor:RGBCOLORWITH16COLOR(0xffffff)];
    
    self.curAcc = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    
    //旧密码输入框
    UITextField *oldTF = [[UITextField alloc] init];
    oldTF.tag = 100001;
    oldTF.frame = CGRectMake(OppositeWidthRate(10), OppositeHeightRate(10), self.view.width - OppositeWidthRate(20), OppositeHeightRate(40));
    
    UIView *oldleftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, OppositeWidthRate(10), OppositeHeightRate(40))];
    oldleftView.backgroundColor = ClearColor;
    oldTF.leftView = oldleftView;
    oldTF.leftViewMode = UITextFieldViewModeAlways;
    oldTF.placeholder = @"当前密码";
    oldTF.textColor = RGBCOLORWITH16COLOR(0x333333);
    oldTF.font = Font15;
    oldTF.secureTextEntry = YES;
    oldTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    oldTF.borderStyle = UITextBorderStyleNone;
    oldTF.background = [UIImage strechingWithImageName:@"update_password_edittext_bg"];
    [self.view addSubview:oldTF];
    self.oldTF = oldTF;
    
    
    //新密码输入框
    UITextField *newTF = [[UITextField alloc] init];
    newTF.tag = 100002;
    newTF.frame = CGRectMake(OppositeWidthRate(10), OppositeHeightRate(20) + CGRectGetMaxY(oldTF.frame), self.view.width - OppositeWidthRate(20), OppositeHeightRate(40));
    UIView *newleftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, OppositeWidthRate(10), 0)];
    newleftView.backgroundColor = ClearColor;
    newTF.leftView = newleftView;
    newTF.leftViewMode = UITextFieldViewModeAlways;
    newTF.placeholder = @"新密码";
    newTF.textColor = RGBCOLORWITH16COLOR(0x333333);
    newTF.font = Font15;
    newTF.secureTextEntry = YES;
    newTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    newTF.borderStyle = UITextBorderStyleNone;
    newTF.background = [UIImage strechingWithImageName:@"update_password_edittext_bg"];
    [self.view addSubview:newTF];
    self.ewTF = newTF;
    
    
    UIButton *errorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [errorBtn setImage:[UIImage imageNamed:@"loginErrorImage"] forState:UIControlStateNormal];
    errorBtn.frame = CGRectMake(OppositeWidthRate(10), OppositeHeightRate(10) + CGRectGetMaxY(newTF.frame), self.view.width - OppositeWidthRate(20), errorBtn.currentImage.size.height);
    errorBtn.titleLabel.font = Font12;
    [errorBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    errorBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    errorBtn.hidden = YES;
    errorBtn.userInteractionEnabled = NO;
    [self.view addSubview:errorBtn];
    self.errorBtn = errorBtn;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!IsIOS7) {
        self.navigationController.navigationBar.tintColor = RGBCOLORWITH16COLOR(0x354d6c);
    } else {
        self.navigationController.navigationBar.barTintColor = RGBCOLORWITH16COLOR(0x354d6c);
    }
    
}


-(void)setErrStatus:(BOOL)status errorMsg:(NSString *)errorMsg
{
    if(status)
    {
        [self.errorBtn setTitle:errorMsg forState:UIControlStateNormal];
        self.errorBtn.hidden = NO;
    }
    else
    {
        self.errorBtn.hidden = YES;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)isSameWithOld
{
    if (self.oldTF.text.length > 0) {
        if ([self.curAcc.passWord isEqualToString:self.oldTF.text]) {
            return YES;
        } else {
            [self setErrStatus:YES errorMsg:@"旧密码不正确"];
            return NO;
        }
    } else {
        [self setErrStatus:YES errorMsg:@"请输入旧密码"];
        return NO;
    }
}

- (BOOL)isAvailableNewPwd
{
    if (self.ewTF.text.length > 0) {
        return YES;
    } else {
        [self setErrStatus:YES errorMsg:@"请输入新密码"];
        return NO;
    }

}

- (void)updatePassWord
{
    if (![self isSameWithOld]) {
        return;
    }
    if (![self isAvailableNewPwd]) {
        return;
    }
    
    [MBProgressHUD showMessage:nil toView:self.view];
    [NetWorkShare  resetPWDWithUserId:self.curAcc.userId newPwd:_ewTF.text oldPwd:_oldTF.text successHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [MBProgressHUD hideHUDForView:self.view];
        if (responseObject != nil) {
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                self.curAcc.passWord = self.ewTF.text;
                [[CFolderMgr shareInstance].accMgr updateAccount:self.curAcc];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } else {
            [MBProgressHUD showError:@"未知错误"];
        }

        
    } failureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络错误"];
    }];
}

//- (BOOL)isSurePassword
//{
//    if (self.newTF.text.length > 0 && _new2PaswordTF.text.length > 0) {
//        if ([_new1PaswordTF.text isEqualToString:_new2PaswordTF.text]) {
//            return YES;
//        } else {
//            return NO;
//        }
//        
//    } else {
//        return NO;
//    }
//}


@end
